//
//  ViewController.m
//  25_多线程
//
//  Created by li cheng on 16/8/17.
//  Copyright © 2016年 li cheng. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *myImageView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    // iOS 下的多线程
    // 1. 进程和进程
    // 什么是进程
    // 进程是进程实体的运行过程，是系统进行资源分配和调度的一个独立单位。
    // 进程是当用户下达执行应用程序的命时,所启动的东西
    // 什么是线程? 线程是进程的执行单位, 线程比作工人
    // 2. 多线程
    // 为了更好的进行一些程序的操作, 节省时间, 提高效率
    // 一个工程不能过多的创建线程, 资源消耗过大, (工厂不能有太多的工人, 工资发不起) 开辟五个左右的线程
    // 注意: 所有UI的操作都放在主线程中进行
    // 需要开辟线程的操作, 图像文字渲染, 大数据储存, 网络请求
    // 3.NSThread
    // 1> currentThread 获取当前线程
    // name:线程名 number 线程编号
    // main 代表主线程
    NSThread *thread = [NSThread currentThread];
    NSLog(@"+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++%@", thread);
    // 2> 开辟一个子线程
    // 子线程随机创建
    [NSThread detachNewThreadSelector:@selector(newThread) toTarget:self withObject:nil];
    [NSThread detachNewThreadWithBlock:^{
        NSThread *new1Thread = [NSThread currentThread];
        NSLog(@"%@", new1Thread);
    }];
    if ([NSThread isMultiThreaded]) {
        NSLog(@"00000000000000000000000000000");
    }
    // 3> 延迟当前线程的执行
//    NSDate *date = [NSDate dateWithTimeIntervalSinceNow:10];
//    [NSThread sleepUntilDate:date];
    // 4> 相对时间
//    [NSThread sleepForTimeInterval:10];
    NSLog(@"111");
    // 5> 不要退出主线程
//    [NSThread exit];
    // 6. threadProrit线程优先级
    double prorit = [NSThread threadPriority];
    NSLog(@"%lf", prorit);
    [NSThread mainThread].name = @"主线程";
    NSLog(@"+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++%@", thread);
    // 8> 判断当前线程
    // 判断当前线程是不是主线程
    [NSThread isMainThread];
    // 判断线程对象是不是主线程
    [[NSThread mainThread] isMainThread];
    // 9> 初始化方法, 需要手动管理线程的声明周期
    // 初始化一个为启动的线程
    NSThread *newThread = [[NSThread alloc] initWithTarget:self selector:@selector(myThread) object:nil];
    [newThread start];
    // 10> 将耗时的操作, 放入后台,不关心线程问题
    [self performSelectorInBackground:@selector(loadImage) withObject:nil];
    

}
- (void)loadImage{
    NSLog(@"-----------------开始");
    NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://pic1.win4000.com/wallpaper/8/511ce1c9ee276.jpg"]];
    _myImageView.image = [UIImage imageWithData:data];
}
- (void)createIamgeView{
    NSLog(@"-----------------开始");
    NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://pic1.win4000.com/wallpaper/8/511ce1c9ee276.jpg"]];
    // 返回主线程, 显示图片
    // 11.返回主线程的操作
//      [self performSelectorOnMainThread:@selector(onMainThreadHandleImage:) withObject:data waitUntilDone:YES];
    NSLog(@"结束了");
}
//- (void)onMainThreadHandleImage:(id)object{
//    _myImageView.image = [UIImage imageWithData:object];
//}
- (void)myThread{
//    for (int i = 0; i < 1000; i++) {
//        if (i == 999) {
//            [NSThread exit];
//        }
//        NSLog(@"%s", __FUNCTION__);
//    }
//  
    
    [self createIamgeView];
}
- (void)newThread{
    NSThread *newThread = [NSThread currentThread];
    NSLog(@"%@", newThread);
    newThread.name = @"王道林的线程";
    double prorit = [NSThread threadPriority];
    NSLog(@"%lf", prorit);
}
- (IBAction)clicked:(id)sender {
    for (int i = 0; i < 1000; i++) {
        NSLog(@"循环了%d次", i);
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
